using System;
using System.Collections.Generic;

namespace FrameIdentityReq.Frames
{
    public static class BaseFrameExtention
    {
        public static Dictionary<FrameTypeEnum, Func<BaseFrame, BaseFrame>> RealFrameDict { get; }

        static BaseFrameExtention()
        {
            RealFrameDict = new Dictionary<FrameTypeEnum, Func<BaseFrame, BaseFrame>>();
            
            RealFrameDict.Add(FrameTypeEnum.NULL, (frame) => frame);

            RealFrameDict.Add(FrameTypeEnum.IDENTITY_REQ,
                (frame) => new IdentityReqFrame(frame.Buffer));
            
            RealFrameDict.Add(FrameTypeEnum.IDENTITY_ACK,
                (frame) => new IdentityAckFrame(frame.Buffer));
        }

        public static BaseFrame ToRealFrame(this BaseFrame frame)
        {
            Func<BaseFrame, BaseFrame> rebuild;
            bool hasFunc = RealFrameDict.TryGetValue(frame.FrameType.Value, out rebuild);
            return hasFunc ? rebuild(frame) : null;
        }
    }
}